<template>
  <el-dialog
      v-model="ledgerEditWinDialog"
      title="编辑台账"
      width="50%">
    <template #default>
      <el-form :model="editLedger" label-width="auto">
        <el-form-item label="设备编码">
          <el-input v-model="editLedger.machineCode"/>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="editLedger.machineName"/>
        </el-form-item>
        <el-form-item label="品牌">
          <el-input v-model="editLedger.machineBrand"/>
        </el-form-item>
        <el-form-item label="规格型号">
          <el-input v-model="editLedger.machineSpec"/>
        </el-form-item>
        <el-form-item label="所属车间">
          <el-select v-model="editLedger.workShopId"/>
          <el-option
            v-for="shop in workShops"
            :key="shop.workShopId"
            :label="shop.workShopName"
            :value="shop.workShopId">
          </el-option>
<!--          <el-input v-model="editLedger.workShopId"/>-->
        </el-form-item>
        <el-form-item label="设备状态">
          <el-select style="width: 100px" v-model="editLedger.machineStatus">
            <el-option :value="1" label="启用"/>
            <el-option :value="2" label="禁用"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editLedger.machineNotes"/>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="editLedgerSubmit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>

import {onMounted, ref, watch} from "vue";
import ledgerRequest from "@/api/ledger.js";
import {ElMessage} from "element-plus";
const props = defineProps(['machineId']);
const emits = defineEmits(['editComplate']);
let ledgerEditWinDialog = ref(false);
let editLedger = ref({});
let workShops = ref({});
function loadWorkShop(){
  ledgerRequest.selectByWorkShop(res=>{
    workShops.value = res.data;
  })
}
function cancel(){
  editLedger = ref({});
  ledgerEditWinDialog.value =false;
}
function editLedgerSubmit(){
  ledgerRequest.updateLedger(editLedger.value,res=>{
    if (res.code == 200){
      ElMessage({
        type:"success",
        message:"修改成功"
      });
      cancel()
      emits('editComplate');
    }else {
      ElMessage({
        type:"warning",
        message:"修改失败"
      })
    }
  })
}

function loadLedgerById(){
  ledgerRequest.selectById(props.machineId,res=>{
    if (res.data){
      editLedger.value =res.data;
      ledgerEditWinDialog.value = true;
    }
  })
}

watch(()=>props.machineId,()=>{
  if(props.machineId){
    loadLedgerById();
  }
});



</script>

<style scoped>

</style>